package com.xwb.controller;

import com.xwb.entity.User;
import com.xwb.entity.Warehouse;
import com.xwb.service.AdmiService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequiresRoles("1")
public class AdmiController {
    @Autowired
    private AdmiService admiService;

    @RequestMapping("/admiQueryWareHouse")
    public String admiQureyWareHouse(Model model){
        List<Warehouse> warehouses = admiService.admiQureyAllWarHouse();
        model.addAttribute("List",warehouses);
        return "administratorQueryWareHousePage";
    }

    @RequestMapping(value ="/admiUpdateWareHouse",method= RequestMethod.POST)
    public String admiUpdateWareHouse(@ModelAttribute Warehouse warehouse){
        admiService.updateWareHouse(warehouse);
        return "redirect:/admiQueryWareHouse";
    }

    @RequestMapping("/admiQueryUser")
    public String admiQueryUser(Model model){
        List<User> users = admiService.admiQueryAllUser();
        model.addAttribute("List",users);
        return "admiQueryUserPage";
    }

    @RequestMapping(value = "/admiUpdateUser",method= RequestMethod.POST)
    public String admiUpdateUser(@ModelAttribute User user){
        admiService.updateUser(user);
        return "redirect:/admiQueryUser";
    }

    @RequestMapping("/admiQueryAllInstorage")
    public String admiQueryAllInstorage(Model model){
        List<HashMap<String, Object>> hashMaps = admiService.admiQueryAllInstorage();
        model.addAttribute("List",hashMaps);
        return "admiQueryAllInstoragePage";
    }

    @RequestMapping("/admiQueryAllOutstorage")
    public String admiQueryAllOutstorage(Model model){
        List<Map<String, Object>> hashMaps = admiService.admiQueryAllOutstorage();
        model.addAttribute("List",hashMaps);
        return "admiQueryAllOutstoragePage";
    }

    @RequestMapping("/admiDeleteWareHouse/{productId}")
    public String admiDeleteWareHouse(@PathVariable("productId") Integer productId){
        admiService.deleteWareHouse(productId);
        return "redirect:/admiQueryWareHouse";
    }

    @RequestMapping("/admiDeleteUser/{userid}")
    public String admiDeleteUser(@PathVariable("userid") Integer userId){
       admiService.deleteUser(userId);
       return "redirect:/admiQueryUser";
    }
}
